//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.neurosky.thinkgear;

import android.util.Log;
import java.util.Arrays;
import org.json.JSONArray;
import org.json.JSONException;

public class EkgEpoch implements Cloneable {
    public int numberOfSamples;
    public float[] data;

    public EkgEpoch(int var1) {
        this.numberOfSamples = var1;
        this.data = new float[this.numberOfSamples];
    }

    public EkgEpoch(int var1, float[] var2) {
        this.numberOfSamples = var1;
        this.data = new float[this.numberOfSamples];
        System.arraycopy(var2, 0, this.data, 0, this.numberOfSamples);
    }

    public EkgEpoch(float[] var1) {
        this.numberOfSamples = var1.length;
        this.data = new float[this.numberOfSamples];
        System.arraycopy(var1, 0, this.data, 0, this.numberOfSamples);
    }

    public EkgEpoch(EkgEpoch var1) {
        this.numberOfSamples = var1.numberOfSamples;
        this.data = new float[this.numberOfSamples];
        System.arraycopy(var1.data, 0, this.data, 0, this.numberOfSamples);
    }

    public EkgEpoch(JSONArray var1) {
        this.numberOfSamples = var1.length();
        this.data = new float[this.numberOfSamples];

        try {
            for(int var2 = 0; var2 < this.numberOfSamples; ++var2) {
                this.data[var2] = (float)var1.getDouble(var2);
            }

        } catch (JSONException var3) {
            Log.d("EkgEpoch", "JSON exception: " + var3.getLocalizedMessage());
        }
    }

    public JSONArray toJSONArray() {
        JSONArray var1 = new JSONArray();
        float[] var2 = this.data;
        int var3 = this.data.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            float var5 = var2[var4];

            try {
                var1.put((double)var5);
            } catch (JSONException var6) {
                var6.printStackTrace();
            }
        }

        return var1;
    }

    public static float[] convolve(float[] var0, float[] var1) {
        float[] var2 = new float[var0.length + var1.length - 1];

        for(int var3 = 0; var3 < var1.length; ++var3) {
            for(int var4 = 0; var4 < var0.length; ++var4) {
                var2[var3 + var4] += var1[var3] * var0[var4];
            }
        }

        return var2;
    }

    public boolean detectHighTail(int var1) {
        boolean var2 = false;
        ++var1;
        int var3;
        int var4 = (var3 = this.data.length / 2) + 1;
        if(var1 < var3) {
            if(this.exceedValue(this.data[var4 - 1], 0, var3 - var1)) {
                var2 = true;
            }
        } else if(this.exceedValue(this.data[var4 - 1], this.data.length - (var3 - (2 * var3 - var1)) - 1, this.data.length)) {
            var2 = true;
        }

        return var2;
    }

    public float getLineNoiseAmplitude() {
        EkgEpoch var1 = this.subtract(this.mean());
        float[] var2 = new float[13];
        float[] var3 = new float[13];

        for(int var4 = 0; var4 < 13; ++var4) {
            float var5 = (float)((double)(2 * var4) * 3.141592653589793D / 13.0D);
            var2[var4] = (float)Math.cos((double)(var5 * 2.0F));
            var3[var4] = (float)Math.sin((double)(var5 * 2.0F));
        }

        float[] var8 = convolve(var1.data, var2);
        float[] var9 = convolve(var1.data, var3);
        float var6 = 0.0F;

        for(int var7 = 0; var7 < var8.length; ++var7) {
            var6 = (float)((double)var6 + Math.sqrt((double)(var8[var7] * var8[var7] + var9[var7] * var9[var7])));
        }

        return var6;
    }

    public EkgEpoch subtract(EkgEpoch var1) {
        EkgEpoch var2 = new EkgEpoch(this.numberOfSamples);

        for(int var3 = 0; var3 < this.numberOfSamples; ++var3) {
            var2.data[var3] = this.data[var3] - var1.data[var3];
        }

        return var2;
    }

    public EkgEpoch subEpoch(int var1, int var2) {
        EkgEpoch var3 = new EkgEpoch(var2 - var1);

        for(int var4 = var1; var4 < var2; ++var4) {
            var3.data[var4 - var1] = this.data[var4];
        }

        return var3;
    }

    public EkgEpoch subEpoch2(int var1, int var2, int var3, int var4) {
        EkgEpoch var5 = new EkgEpoch(var2 - var1 + var4 - var3);
        int var6 = 0;

        for(var1 = var1; var1 < var2; ++var1) {
            var5.data[var6++] = this.data[var1];
        }

        for(var1 = var3; var1 < var4; ++var1) {
            var5.data[var6++] = this.data[var1];
        }

        return var5;
    }

    public EkgEpoch square() {
        EkgEpoch var1 = new EkgEpoch(this.numberOfSamples);

        for(int var2 = 0; var2 < this.numberOfSamples; ++var2) {
            var1.data[var2] = this.data[var2] * this.data[var2];
        }

        return var1;
    }

    public EkgEpoch subtract(float var1) {
        EkgEpoch var2 = new EkgEpoch(this.numberOfSamples);

        for(int var3 = 0; var3 < this.numberOfSamples; ++var3) {
            var2.data[var3] = this.data[var3] - var1;
        }

        return var2;
    }

    public EkgEpoch diff() {
        EkgEpoch var1 = new EkgEpoch(this.numberOfSamples - 1);

        for(int var2 = 0; var2 < var1.numberOfSamples; ++var2) {
            var1.data[var2] = this.data[var2 + 1] - this.data[var2];
        }

        return var1;
    }

    public boolean exceedValue(float var1, int var2, int var3) {
        for(var2 = var2; var2 < var3; ++var2) {
            if(this.data[var2] > var1) {
                return true;
            }
        }

        return false;
    }

    public EkgEpoch smooth(int var1) {
        EkgEpoch var2 = new EkgEpoch(this.numberOfSamples);
        if((float)Math.round((float)var1 / 2.0F) == (float)var1 / 2.0F) {
            --var1;
        }

        for(int var3 = 0; var3 < var2.numberOfSamples; ++var3) {
            int var4;
            int var5 = ((var4 = Math.min(Math.min(var3, var1 / 2), var2.numberOfSamples - var3 - 1)) << 1) + 1;

            for(int var6 = -var4; var6 < var4 + 1; ++var6) {
                var2.data[var3] += this.data[var3 + var6];
            }

            var2.data[var3] /= (float)var5;
        }

        return var2;
    }

    public float mean() {
        return this.sum() / (float)this.numberOfSamples;
    }

    public float sum() {
        float var1 = 0.0F;

        for(int var2 = 0; var2 < this.numberOfSamples; ++var2) {
            var1 += this.data[var2];
        }

        return var1;
    }

    public float max() {
        float var1 = 0.0F;

        for(int var2 = 0; var2 < this.numberOfSamples; ++var2) {
            if(this.data[var2] > var1) {
                var1 = this.data[var2];
            }
        }

        return var1;
    }

    public float median() {
        float[] var1 = new float[this.numberOfSamples];

        for(int var2 = 0; var2 < this.numberOfSamples; ++var2) {
            var1[var2] = this.data[var2];
        }

        Arrays.sort(var1);
        return Math.floor((double)(this.numberOfSamples / 2)) * 2.0D == (double)this.numberOfSamples?(var1[(int)Math.ceil((double)(this.numberOfSamples / 2)) - 1] + var1[(int)Math.ceil((double)(this.numberOfSamples / 2))]) / 2.0F:var1[(int)Math.ceil((double)(this.numberOfSamples / 2))];
    }

    public EkgEpoch clone() {
        try {
            return (EkgEpoch)super.clone();
        } catch (Exception var1) {
            return null;
        }
    }

    public EkgEpoch sort() {
        EkgEpoch var1;
        Arrays.sort((var1 = new EkgEpoch(this)).data);
        return var1;
    }

    public int[] sortIndices(EkgEpoch var1) {
        int[] var2 = new int[this.numberOfSamples];

        for(int var3 = 0; var3 < this.numberOfSamples; ++var3) {
            for(int var4 = 0; var4 < this.numberOfSamples; ++var4) {
                if(this.data[var3] == var1.data[var4]) {
                    var2[var4] = var3;
                    break;
                }
            }
        }

        return var2;
    }

    public float std() {
        float var1 = 0.0F;
        float var2 = this.mean();

        for(int var3 = 0; var3 < this.numberOfSamples; ++var3) {
            var1 += (this.data[var3] - var2) * (this.data[var3] - var2);
        }

        return (float)Math.sqrt((double)(var1 / (float)(this.numberOfSamples - 1)));
    }

    public int find_heart_beats(int[] var1, float var2) {
        int var3 = Math.abs(var1[0]);
        int var11 = var1[1];
        EkgEpoch var4;
        EkgEpoch var5 = (var4 = this.subtract(this.median())).diff();
        int var6 = 0;
        float var7 = 0.0F;

        for(int var8 = var3; var8 < var5.numberOfSamples - var11; ++var8) {
            if(var5.data[var8 - 1] > 0.0F & var5.data[var8] < 0.0F) {
                float var9 = var4.data[var8] - var4.data[var8 - var3] + (var4.data[var8] - var4.data[var8 + var11]);
                float var10 = var4.data[var8 + 1] - var4.data[var8 + 1 - var3] + (var4.data[var8 + 1] - var4.data[var8 + 1 + var11]);
                if(var9 > var7) {
                    var7 = var9;
                    var6 = var8;
                }

                if(var10 > var7) {
                    var7 = var10;
                    var6 = var8 + 1;
                }
            }
        }

        if(var7 < var2) {
            return -1;
        } else if((double)(var4.data[var6] - var4.data[var6 - var3]) < (double)var2 / 2.8D | var4.data[var6] - var4.data[var6 + var11] < var2 / 2.0F) {
            return -1;
        } else {
            return var6;
        }
    }

    public EkgEpoch detrend() {
        float[] var1 = this.data;
        float[] var2 = new float[this.numberOfSamples];
        int var3 = this.numberOfSamples;
        float var4 = 0.0F;
        float var5 = 0.0F;

        for(int var6 = 0; var6 < var3; ++var6) {
            var2[var6] = (float)var6;
            var4 += var2[var6];
            var5 += var1[var6];
        }

        float var11 = var4 / (float)var3;
        var4 = var5 / (float)var3;
        var5 = 0.0F;
        float var7 = 0.0F;

        for(int var8 = 0; var8 < var3; ++var8) {
            var5 += (var2[var8] - var11) * (var2[var8] - var11);
            var7 += (var2[var8] - var11) * (var1[var8] - var4);
        }

        float var12 = var7 / var5;
        float var9 = var4 - var12 * var11;
        EkgEpoch var10 = new EkgEpoch(this.numberOfSamples);

        for(var3 = 0; var3 < var10.numberOfSamples; ++var3) {
            var10.data[var3] = this.data[var3] - var9 - var12 * (float)var3;
        }

        return var10;
    }
}
